package com.mall.shopping.services;

import com.mall.shopping.ITestProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.TestProductDetailConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.TestProductDetailDto;
import com.mall.shopping.dto.TestProductRequest;
import com.mall.shopping.dto.TestProductResponse;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class ITestProductServiceImpl implements ITestProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    TestProductDetailConverter productDetailConverter;

    @Override
    public TestProductResponse getProductDetail(TestProductRequest request) {

        TestProductResponse testProductResponse = new TestProductResponse();

        try {
            // 有效参数校验
            request.requestCheck();

            Item item = itemMapper.selectByPrimaryKey(request.getProductId());
            TestProductDetailDto testProductDetailDto
                    = productDetailConverter.testProductToProductDto(item);

            testProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            testProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            testProductResponse.setProductDetailDto(testProductDetailDto);
        } catch (Exception e) {
            testProductResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            testProductResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }

        return testProductResponse;
    }
}
